package ch4tree;

import java.util.ArrayList;
import java.util.LinkedList;

public class Ex4 {
	static ArrayList<LinkedList<TreeNode>> findLevelLinkList(TreeNode root) {
		ArrayList<LinkedList<TreeNode>> result = new ArrayList<>();
		if (root == null) {
			return result;
		}
		
		LinkedList<TreeNode> list = new LinkedList<>();
		list.add(root);
		result.add(list);
		int level = 0;
		
		while (list.size() > 0) {
			LinkedList<TreeNode> parents = result.get(level++);
			list = new LinkedList<>();
			for (TreeNode parent : parents) {
				TreeNode left = parent.left;
				if (left != null) {
					list.add(left);
				}
				TreeNode right = parent.right;
				if (right != null) {
					list.add(right);
				}
			}
			result.add(list);
		}
		
		return result;
	}
	
	public static void main(String ... argv) {
		TreeNode node = Chapter.createMinimalBST(new int [] {1,2,3,4,5,6});
		BTreePrinter.printNode(node);
		ArrayList<LinkedList<TreeNode>> findLevelLinkList = findLevelLinkList(node);
		int a=1;
	}
}
